<?php
/**
 * 小程序转介系统
 */
namespace app\mini\controller;
class Transfer extends Pre
{
    public function lists(){
        $data = $this -> data;
        $loginInfo = $this -> loginInfo;
        //加入已读缓存
        $cacheNum = cache('mini_num');
        $cacheNum['transfer'] = \app\common\model\Transfer::where('to_account_id',$loginInfo['id'])->column('id');
        cache('mini_num',$cacheNum,3600*24*365);

        $myuser = \app\common\model\Message::where('account_id',$loginInfo['id'])->column('id');
        //获取我和我开的账号
        $allAcount = Account::allAcount($loginInfo['id']);
        //被撤回
        switch ($data['mark']){
            //推荐被撤回
            case '1':
                $findMap[] = ['to_account_id','in',$allAcount];
                $findDel = 'onlyTrashed';
                break;
                //转出客户
            case '2':
                $findMap[] = ['message_id','in',$myuser];
                $findDel = 'where';
                break;
                //撤回转出客户
            case '3':
                $findMap[] = ['message_id','in',$myuser];
                $findDel = 'onlyTrashed';
                break;
                //同事推荐
            default:
                $findMap[] = ['to_account_id','in',$allAcount];
                $findDel = 'where';
        }

        if ($data['agent'])
            $findMap[] = ['to_account_id','eq',$data['agent']];
        //构建搜索条件
        if ($data['key'])
            $findMessage[] = ['name|telephone','like',"%$data[key]%"];
        if ($data['building'])
            $findMessage[] = ['building','eq',$data['building']];
        if ($data['status'])
            $findMessage[] = ['status','eq',$data['status']];
        if ($data['endDate']){
            if ($data['startDate']){
                $findMessage[] = ['time','between',[strtotime($data['startDate']),strtotime($data['endDate'])]];
            }
            else{
                $findMessage[] = ['time','elt',strtotime($data['endDate'])];
            }
        }
        else{
            if ($data['startDate'])
                $findMessage[] = ['time','egt',strtotime($data['startDate'])];
        }
        if ($findMessage){
            $allMessage = \app\common\model\Message::where($findMessage)->column('id');
            $findMap[] = ['message_id','in',$allMessage];
        }

        $info = \app\common\model\Transfer::
        $findDel([])->
        with([
            'messageInfo'=>
                function($query){
                    $query->field('name,telephone,id,status,building');
                },
            'accountInfo','buildingInfo'
        ])
            -> where($findMap)
            ->limit("$data[start],$data[count]")
            ->order('date desc')
            -> select();
        foreach ($info as $k=> $value){
            $value['message_info'] -> buildingInfo;
            $info[$k]['date'] = date('Y-m-d H:i:s',$value['date']);
            $info[$k]['delete_time'] = date('Y-m-d H:i:s',$value['delete_time']);
            if(!in_array($data['mark'],['2','3']))
                if ($value['to_account_id']!=$loginInfo['id'])
                    if (!in_array($loginInfo['role'],['21','30']) )
                        $info[$k]['message_info']['telephone'] = substr_replace($value['message_info']['telephone'],'****',3,4);
        }

        $num = \app\common\model\Transfer:: $findDel([])->where($findMap)->count();
        return [
            'content' => $info,
            'num' => $num,
        ];
    }

    //外部转介
    public function inserts(){
        $transfer =new \app\common\model\Transfer();
        return $transfer -> inserts($this -> loginInfo);
    }

    //内部转介
    public function inner_inserts(){
        $transfer =new \app\common\model\Transfer();
        return $transfer -> inner_inserts($this -> loginInfo);
    }

    public function dels(){
        $transfer =new \app\common\model\Transfer();
        return $transfer -> dels();
    }
}